import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index'

Vue.use(Router)
//获取原型对象上的push函数
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

/* Layout */
import Layout from '../views/layout/Layout'
import Layout2 from '../views/layout/Layout2'
import Layout3 from '../views/layout/Layout3'
import VueCryptojs from 'vue-cryptojs'

Vue.use(VueCryptojs)
// import signList from '../views/home/signList'

/**
 * hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
 *                                if not set alwaysShow, only more than one route under the children
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
  }
 **/
export const router = new Router({
  routes: [
    {
      path: '/',
      component: () => import('@/views/login/index'),
      hidden: true
    },
    {
      path: '/share',
      component: () => import('@/views/shareCode/index'),
      hidden: true
    },
    {
      path: '/404',
      component: () => import('@/views/404'),
      meta:{
        roles: ['admin','user','major','xueyuan','school']
      },
      hidden: true
    },
    //系统管理员页面
    {
      path: '/home',
      component: Layout3,
      // redirect: '/home',
      meta: {
        title: '首页',
        icon: 'el-icon-s-custom',
        roles: ['admin']
      },
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('@/views/home/index'),
          meta: {
            title: '首页',
            icon: 'el-icon-s-custom',
            roles: ['admin']
          }
        },
        {
          path: 'memberList',
          name: 'memberList',
          component: Layout2,
          // component: () => import('@/views/home/signList'),
          meta: {
            title: '签到活动',
            roles: ['admin']
          },
          redirect: '/home/memberList/memberList',
          hidden: true,
          children: [
            {
              path: 'memberList',
              name: 'memberList',
              component: () => import('@/views/home/memberlist/index'),
              meta: {
                title: '签到活动',
                roles: ['admin']
              },
            },
            {
              path: 'editmemberList',
              name: 'editmemberList',
              component: () => import('@/views/home/memberlist/editlist'),
              meta: {
                title: '签到活动',
                roles: ['admin']
              },
            },
          ]
        },
        {
          path: 'signList',
          name: 'signList',
          component: Layout2,
          // component: () => import('@/views/home/signList'),
          meta: {
            title: '签到活动',
            roles: ['admin']
          },
          redirect: '/home/signList/signList',
          hidden: true,
          children: [
            {
              path: 'signList',
              name: 'signList',
              component: () => import('@/views/home/signList'),
              meta: {
                title: '签到活动',
                roles: ['admin']
              },
            },
            {
              path: 'signlinkList',
              name: 'signlinkList',
              component: Layout2,
              meta: {
                title: '签到环节',
                roles: ['admin']
              },
              hidden: true,
              redirect: '/home/signList/signlinkList/signlinkList',
              children: [
                {
                  path: 'signlinkList',
                  name: 'signlinkList',
                  component: () => import('@/views/home/signlinkList'),
                  meta: {
                    title: '签到环节',
                    roles: ['admin']
                  },
                  hidden: true
                },
                {
                  path: 'signResult',
                  name: 'signResult',
                  component: () => import('@/views/home/signResult'),
                  meta: {
                    title: '签到结果',
                    roles: ['admin']
                  },
                  hidden: true
                }
              ]
            }
          ]
        }
      ]
    },
    //系统管理员审批
    {
      path: '/shenpi',
      component: Layout3,
      redirect: '/shenpi/index',
      name: 'pms',
      meta: {
        title: '商品',
        icon: 'product',
        roles: ['admin']
      },
      children:[
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/home/schooladminapply'),
          meta: {
            title: '审批申请',
            icon: 'el-icon-s-check',
            roles: ['admin']
          }
        }
      ]

    },
    //商品管理
    // {
    //   path: '/pms',
    //   component: Layout,
    //   redirect: '/pms/product',
    //   name: 'pms',
    //   meta: {
    //     title: '商品',
    //     icon: 'el-icon-s-shop',
    //     roles: ['admin']
    //   },
    //   children: [
    //     {
    //       path: 'product',
    //       name: 'product',
    //       component: () => import('@/views/pms/product/index'),
    //       meta: {
    //         title: '商品列表',
    //         icon: 'el-icon-s-order',
    //         roles: ['admin']
    //       }
    //     },
    //     {
    //       path: 'addProduct',
    //       name: 'addProduct',
    //       component: () => import('@/views/pms/product/add'),
    //       meta: {
    //         title: '兑换记录',
    //         icon: 'el-icon-s-platform',
    //         roles: ['admin']
    //       }
    //     },
    //     {
    //       path: 'updateProduct',
    //       name: 'updateProduct',
    //       component: () => import('@/views/pms/product/update'),
    //       meta: {
    //         title: '发货情况',
    //         icon: 'el-icon-s-opportunity',
    //         roles: ['admin']
    //       }
    //     }
    //   ]
    // },
    // {
    //   path: '/pms',
    //   component: Layout3,
    //   name: 'pms',
    //   meta: {
    //     title: '管理',
    //     icon: 'product-cate',
    //     roles: ['admin']
    //   },
    //   children: [
    //     {
    //       path: 'admin_List',
    //       name: 'admin_List',
    //       component: () => import('@/views/pms/product/adminList'),
    //       meta: {
    //         title: '管理',
    //         icon: 'product-cate',
    //         roles: ['admin']
    //       }
    //     }
    //   ]
    // },

    //个人签到活动
    {
      path: '/signList',
      meta: {
        title: '个人签到活动',
        icon: 'home',
        roles: ['user','xueyuan','school','major']
      },
      component: Layout3,
      children: [
        {
          path: 'signList',
          name: 'signList',
          component: () => import('@/views/user/mysignList'),
          meta: {
            title: '个人签到活动',
            icon: 'el-icon-user-solid',
            roles: ['user','xueyuan','school','major']
          }
        },
        {
          path: 'signlinkList',
          name: 'signlinkList',
          component: Layout2,
          meta: {
            title: '签到环节',
            roles: ['user','xueyuan','school','major']
          },
          hidden: true,
          redirect: '/signList/signlinkList/signlinkList',
          children: [
            {
              path: 'signlinkList',
              name: 'signlinkList',
              component: () => import('@/views/user/mysignlinkList'),
              meta: {
                title: '签到环节',
                roles: ['user','xueyuan','school','major']
              },
              hidden: true
            },
            {
              path: 'signCode',
              name: 'signCode',
              component: () => import('@/views/user/mysignCode'),
              meta: {
                title: '签到二维码',
                roles: ['user','xueyuan','school','major']
              },
              hidden: true
            },
            {
              path: 'signResult',
              name: 'signResult',
              component: () => import('@/views/user/mysignResult'),
              meta: {
                title: '签到结果',
                roles: ['user','xueyuan','school','major']
              },
              hidden: true
            }
          ]
        }
      ]
    },
    //各类用户的签到名单
    {
      path: '/memberlist',
      component: Layout3,
      meta: {
        title: '签到名单',
        icon: 'home',
        roles: ['user','xueyuan','major','school']
      },
      redirect:'/memberlist/memberlist1',
      children: [
        {
          path: 'memberlist1',
          name: 'memberlist1',
          component: () => import('@/views/user/sharefunction/memberlist/index'),
          meta: {
            title: '签到名单',
            icon: 'el-icon-s-order',
            roles: ['user','xueyuan','major','school']
          }
        },
        {
          path: 'memberlist2',
          name: 'memberlist2',
          component: () => import('@/views/user/sharefunction/memberlist/editlist'),
          meta: {
            title: '修改名单',
            icon: 'home',
            roles: ['user','xueyuan','major','school']
          },
          hidden:true
        },
      ]
    },
    //各类用户的签到模板
    {
      path: '/signtemp',
      component: Layout3,
      meta: {
        title: '签到模板',
        icon: 'home',
        roles: ['user','xueyuan','major','school']
      },
      redirect:'/signtemp/index',
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/user/sharefunction/signtemplate/index'),
          meta: {
            title: '签到模板',
            icon: 'el-icon-menu',
            roles: ['user','xueyuan','major','school']
          }
        },
        {
          path: 'signlist',
          name: 'signlist',
          component: () => import('@/views/user/sharefunction/signtemplate/signList'),
          meta: {
            title: '签到列表',
            icon: 'el-icon-menu',
            roles: ['user','xueyuan','major','school']
          },
          hidden:true
        },
        {
          path: 'signlinklist',
          name: 'signlinklist',
          component: () => import('@/views/user/sharefunction/signtemplate/signlinkList'),
          meta: {
            title: '签到列表',
            icon: 'el-icon-menu',
            roles: ['user','xueyuan','major','school']
          },
          hidden:true
        },
        {
          path: 'signcode',
          name: 'signcode',
          component: () => import('@/views/user/sharefunction/signtemplate/signCode'),
          meta: {
            title: '签到列表',
            icon: 'el-icon-menu',
            roles: ['user','xueyuan','major','school']
          },
          hidden:true
        },
        {
          path: 'signresult',
          name: 'signresult',
          component: () => import('@/views/user/sharefunction/signtemplate/signResult'),
          meta: {
            title: '签到列表',
            icon: 'el-icon-menu',
            roles: ['user','xueyuan','major','school']
          },
          hidden:true
        }
      ]
    },
    //组织管理员
    {
      path: '/zuzhi',
      component: Layout3,
      meta: {
        title: '组织管理',
        icon: 'el-icon-menu',
        roles: ['major','school','xueyuan']
      },
      children:[
        {
          path: 'home',
          name: 'home',
          component: () => import('@/views/user/sharefunction/teacheract/index'),
          meta: {
            title: '教师签到活动',
            icon: 'el-icon-s-custom',
            roles: ['school','xueyuan','major']
          }
        },
        {
          path: 'signList',
          name: 'signList',
          component: Layout2,
          // component: () => import('@/views/home/signList'),
          meta: {
            title: '签到活动',
            roles: ['school','xueyuan','major']
          },
          redirect: '/zuzhi/signList/signList',
          hidden: true,
          children: [
            {
              path: 'signList',
              name: 'signList',
              component: () => import('@/views/user/sharefunction/teacheract/signList'),
              meta: {
                title: '签到活动',
                roles: ['school','xueyuan','major']
              },
            },
            {
              path: 'signlinkList',
              name: 'signlinkList',
              component: Layout2,
              meta: {
                title: '签到环节',
                roles: ['school','xueyuan','major']
              },
              hidden: true,
              redirect: '/zuzhi/signList/signlinkList/signlinkList',
              children: [
                {
                  path: 'signlinkList',
                  name: 'signlinkList',
                  component: () => import('@/views/user/sharefunction/teacheract/signlinkList'),
                  meta: {
                    title: '签到环节',
                    roles: ['school','xueyuan','major']
                  },
                  hidden: true
                },
                {
                  path: 'signResult',
                  name: 'signResult',
                  component: () => import('@/views/user/sharefunction/teacheract/signResult'),
                  meta: {
                    title: '签到结果',
                    roles: ['school','xueyuan','major']
                  },
                  hidden: true
                }
              ]
            }
          ]
        },
        //专业管理员权限绑定
        {
          path: '/userrightlink1',
          component: Layout2,
          meta: {
            title: '组织绑定',
            icon: 'home',
            roles: ['major']
          },
          redirect:'/zuzhi/userrightlink1/userrightlink11',
          children: [
            {
              path: 'userrightlink11',
              name: 'userrightlink11',
              component: () => import('@/views/user/majoradmin/index'),
              meta: {
                title: '组织绑定',
                icon: 'el-icon-star-on',
                roles: ['major']
              }
            },
          ]
        },
        //学院管理员权限绑定
        {
          path: '/userrightlink2',
          component: Layout2,
          meta: {
            title: '组织绑定',
            icon: 'home',
            roles: ['xueyuan']
          },
          redirect:'/zuzhi/userrightlink2/userrightlink21',
          children: [
            {
              path: 'userrightlink21',
              name: 'userrightlink21',
              component: () => import('@/views/user/xueyuanadmin/index'),
              meta: {
                title: '组织绑定',
                icon: 'el-icon-star-on',
                roles: ['xueyuan']
              }
            },
          ]
        },
        //各类管理员审批申请
        {
          path: '/checkCommonApply2',
          component: Layout2,
          meta: {
            title: '审批申请',
            icon: 'home',
            roles: ['xueyuan','school']
          },
          redirect:'/zuzhi/checkCommonApply2/checkCommonApply21',
          children: [
            {
              path: 'checkCommonApply21',
              name: 'checkCommonApply21',
              component: () => import('@/views/user/sharefunction/zuzhiAdminApply/commonApply'),
              meta: {
                title: '审批申请',
                icon: 'el-icon-s-check',
                roles: ['xueyuan','admin','school']
              }
            },
          ]
        },

        //各类管理员教师管理
        {
          path: '/teacherManage',
          component: Layout2,
          meta: {
            title: '教师管理',
            icon: 'home',
            roles: ['xueyuan','major','school']
          },
          redirect:'/zuzhi/teacherManage/teacherManage1',
          children: [
            {
              path: 'teacherManage1',
              name: 'teacherManage1',
              component: () => import('@/views/user/sharefunction/teachermanage/index'),
              meta: {
                title: '教师管理',
                icon: 'el-icon-s-grid',
                roles: ['xueyuan','major','school']
              }
            },
          ]
        },
        //学校管理员权限绑定
        {
          path: '/userrightlink3',
          component: Layout2,
          meta: {
            title: '组织绑定',
            icon: 'home',
            roles: ['school']
          },
          redirect:'/zuzhi/userrightlink3/userrightlink31',
          children: [
            {
              path: 'userrightlink31',
              name: 'userrightlink31',
              component: () => import('@/views/user/schooladmin/index'),
              meta: {
                title: '组织绑定',
                icon: 'el-icon-star-on',
                roles: ['school']
              }
            },
          ]
        },
      ]
    },
    //权限申请
    {
      path: '/userright',
      component: Layout3,
      meta: {
        title: '组织申请',
        icon: 'home',
        roles: ['user','xueyuan','school','major']
      },
      redirect:'/userright/userright1',
      children: [
        {
          path: 'userright1',
          name: 'userright1',
          component: () => import('@/views/user/userright/index'),
          meta: {
            title: '组织申请',
            icon: 'el-icon-s-promotion',
            roles: ['user','xueyuan','school','major']
          }
        },
      ]
    },
    // //分享二维码链接
    // {
    //   path: '/share',
    //   component: Layout3,
    //   redirect:'/share/sharecode',
    //   children: [
    //     {
    //       path: 'sharecode',
    //       name: 'sharecode',
    //       component: () => import('@/views/shareCode/index'),
    //       meta: {
    //         title: 'sharecode',
    //         icon: 'home',
    //         roles: ['admin']
    //       },

    //     },
    //   ]
    // },
    //
    {
      path: '*',
      redirect: '/404',
      hidden: true
    }
  ]
})
export default router



router.beforeEach((to, from, next) => {
  var roleid=sessionStorage.getItem("role")
  let role='user'
    if(roleid==-1)
      role='admin'
    else if(roleid==1)
      role='school'
    else if(roleid==2)
      role='xueyuan'
    else if(roleid==3)
      role='major'
    const tokenStr = window.sessionStorage.getItem('Authorization')
    const loginValue =  window.sessionStorage.getItem('loginvalue')
    if (to.path === '/') {
      console.log("前往登录")
      return next()
    }
    else if(to.path==='/share')
      return next()
    else{
      if (!tokenStr) {
        return next('/')
      }else{
        console.log("当前的role",role)
        if(to.meta.roles.indexOf(role)>-1){
          next();
        }else{
          console.log("权限不够！！！")
          next({path:"/404"})
        }
      }
    }
})

